<template>
  <el-dialog
    :title="!dataForm.areaCode ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="personalCode">
      <el-input v-model="dataForm.personalCode" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="name">
      <el-input v-model="dataForm.name" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="sex">
      <el-input v-model="dataForm.sex" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="nation">
      <el-input v-model="dataForm.nation" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="politicalFace">
      <el-input v-model="dataForm.politicalFace" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="idNo">
      <el-input v-model="dataForm.idNo" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="birthDate">
      <el-input v-model="dataForm.birthDate" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="workTime">
      <el-input v-model="dataForm.workTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="retirementTime">
      <el-input v-model="dataForm.retirementTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="retirementStatus">
      <el-input v-model="dataForm.retirementStatus" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="duties">
      <el-input v-model="dataForm.duties" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="county">
      <el-input v-model="dataForm.county" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="street">
      <el-input v-model="dataForm.street" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="originalTroops">
      <el-input v-model="dataForm.originalTroops" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="handoverTime">
      <el-input v-model="dataForm.handoverTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="handoverMode">
      <el-input v-model="dataForm.handoverMode" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="handoverBatch">
      <el-input v-model="dataForm.handoverBatch" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="workersClassification">
      <el-input v-model="dataForm.workersClassification" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="myStatus">
      <el-input v-model="dataForm.myStatus" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="death">
      <el-input v-model="dataForm.death" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="deathTime">
      <el-input v-model="dataForm.deathTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="personType">
      <el-input v-model="dataForm.personType" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          areaCode: 0,
          personalCode: '',
          name: '',
          sex: '',
          nation: '',
          politicalFace: '',
          idNo: '',
          birthDate: '',
          workTime: '',
          retirementTime: '',
          retirementStatus: '',
          duties: '',
          county: '',
          street: '',
          originalTroops: '',
          handoverTime: '',
          handoverMode: '',
          handoverBatch: '',
          workersClassification: '',
          myStatus: '',
          death: '',
          deathTime: '',
          personType: ''
        },
        dataRule: {
          personalCode: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          nation: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          politicalFace: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          idNo: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          birthDate: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          workTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          retirementTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          retirementStatus: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          duties: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          county: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          street: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          originalTroops: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          handoverTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          handoverMode: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          handoverBatch: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          workersClassification: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          myStatus: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          death: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          deathTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          personType: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.areaCode = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.areaCode) {
            this.$http({
              url: this.$http.adornUrl(`/jxxm/all/info/${this.dataForm.areaCode}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.personalCode = data.all.personalCode
                this.dataForm.name = data.all.name
                this.dataForm.sex = data.all.sex
                this.dataForm.nation = data.all.nation
                this.dataForm.politicalFace = data.all.politicalFace
                this.dataForm.idNo = data.all.idNo
                this.dataForm.birthDate = data.all.birthDate
                this.dataForm.workTime = data.all.workTime
                this.dataForm.retirementTime = data.all.retirementTime
                this.dataForm.retirementStatus = data.all.retirementStatus
                this.dataForm.duties = data.all.duties
                this.dataForm.county = data.all.county
                this.dataForm.street = data.all.street
                this.dataForm.originalTroops = data.all.originalTroops
                this.dataForm.handoverTime = data.all.handoverTime
                this.dataForm.handoverMode = data.all.handoverMode
                this.dataForm.handoverBatch = data.all.handoverBatch
                this.dataForm.workersClassification = data.all.workersClassification
                this.dataForm.myStatus = data.all.myStatus
                this.dataForm.death = data.all.death
                this.dataForm.deathTime = data.all.deathTime
                this.dataForm.personType = data.all.personType
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/jxxm/all/${!this.dataForm.areaCode ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'areaCode': this.dataForm.areaCode || undefined,
                'personalCode': this.dataForm.personalCode,
                'name': this.dataForm.name,
                'sex': this.dataForm.sex,
                'nation': this.dataForm.nation,
                'politicalFace': this.dataForm.politicalFace,
                'idNo': this.dataForm.idNo,
                'birthDate': this.dataForm.birthDate,
                'workTime': this.dataForm.workTime,
                'retirementTime': this.dataForm.retirementTime,
                'retirementStatus': this.dataForm.retirementStatus,
                'duties': this.dataForm.duties,
                'county': this.dataForm.county,
                'street': this.dataForm.street,
                'originalTroops': this.dataForm.originalTroops,
                'handoverTime': this.dataForm.handoverTime,
                'handoverMode': this.dataForm.handoverMode,
                'handoverBatch': this.dataForm.handoverBatch,
                'workersClassification': this.dataForm.workersClassification,
                'myStatus': this.dataForm.myStatus,
                'death': this.dataForm.death,
                'deathTime': this.dataForm.deathTime,
                'personType': this.dataForm.personType
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
